<template>
	<view class="login">
		<!-- #ifndef H5 -->
		<view style="width:100%;height: var(--status-bar-height);"></view>
		<!-- #endif -->
		<image :src="$util.img($store.state.loginImg)" mode="widthFix"></image>
		<view class="item-input">
			<input type="number" placeholder-class="place-holderClass" v-model="mobile" placeholder="请输入手机号"/>
		</view>
		<view class="item-input">
			<input type="text" placeholder-class="place-holderClass" v-model="code" placeholder="请输入验证码"/>
			<view class="item-time" @click="verification">
				{{ time?time + 's已发送':'获取验证码'}}
			</view>
		</view>
		<view class="item-input">
			<input type="text" password placeholder-class="place-holderClass" v-model="password" placeholder="请输入新密码"/>
		</view>
		<view class="item-input">
			<input type="text" password placeholder-class="place-holderClass" v-model="re_password" placeholder="请确认新密码"/>
		</view>

		<button class="login" type="default" @click="change">确定</button>
	</view>
</template>

<script>
	export default{
		data(){
			return {
				code:'',//验证码
				mobile:'',//手机号
				password:'',//密码
				re_password:'',//确认密码
				time:'',//获取验证码时间
				repeatJudge:false,//防重复
				// 手机号做正则判断
				pattern:/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
			}
		},
		onLoad(option){
		},
		methods:{
			//获取验证码
			verification(){
				let title = '';
				if(!this.pattern.test(this.mobile)) title = '请输入正确的手机号'
				if(!this.mobile) title = '请输入手机号'
				if(title){
					uni.showToast({title:title,icon:'none'})
					return false ;
				}
				this.$http.sendrequest('/login/sms_verification_code',{
					mobile:this.mobile,
					type:'其他',
				}).then(res=>{
					uni.showToast({title:res.msg,icon:'none'})
					if(res.code == 1){
						this.time = 60;
						let die = setInterval(()=>{
							this.time -= 1;
							if(this.time == 0) clearInterval(die) 
						},1000)
					}
				})
			},
			//确定修改
			change(){
				let title = '';
				if(!this.code || !this.mobile || !this.password || !this.re_password) title= '输入请勿为空';
				if(title){
					uni.showToast({title:title,icon:'none'})
					return false
				}
				if(this.repeatJudge) return false;
				this.repeatJudge = true;
				this.$http.sendrequest('/login/forgotPassword',{
					mobile:this.mobile,
					password:this.password,
					re_password:this.re_password,
					code:this.code
				}).then(res=>{
					uni.showToast({title:res.msg,icon:'none'})
					this.repeatJudge = false;
					if(res.code == 1){
						setTimeout(()=>{
							this.$util.redirectTo('/pages/login/login',{},'reLaunch')
						},1000)
					}
				})
				
			},
		}
	}
</script>

<style lang="scss" scoped>
	@import './css/diy-login.scss'
</style>
